<?php

namespace app\teacher\controller;


use app\admin\model\classes\Student;
use think\Db;
use think\Validate;
use app\common\model\ClassesSign as ClassesSignModel;
use app\admin\model\classes\Classes;
use app\common\model\ClassesSignLog;

class ClassesSign extends Base
{
    protected $model = null;
    public function __construct(ClassesSignModel $model)
    {
        parent::__construct();
        $this->model = $model;
    }
    public function index()
    {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);
        $classesId = $this->request->get('classes_id', 0);
        if (!$classesId) $this->error('参数错误');

        $this->success('获取成功', [
            'count' =>  $this->model->where('classes_id', $classesId)->count(),
            'list' =>  $this->model->where('classes_id', $classesId)->page($page, $limit)->select()
        ]);

    }
    
    public function info()
    {
        $id = $this->request->get('id', 0);
        if (!$id) return $this->error('参数错误');

        $info = $this->model->with('classes')->find($id);
        if (!$info) $this->error('数据不存在');
        $info['class_total_person'] = Student::where('classes_id', $info['classes_id'])->count();
        $this->success($info);
    }

    public function edit()
    {
        $id = $this->request->post('id', 0);
        //参数验证
        $rule = [
            'title'   => 'require',
            'sign_end_time' => 'require',
            'type' => 'require',
            'classes_id'  => 'require',
        ];

        $msg = [
            'classes_id.require' => '请选择班级',
            'title.require' => '标题不能为空',
            'type.require' => '请选择签到方式',
            'sign_end_time.require' => '请选择签到截至时间',
        ];
        $data = $this->request->post();
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)) $this->error($validate->getError());

        //验证班级是否有效
        $classes = Classes::where('id', $data['classes_id'])->find();
        if (!$classes) $this->error('班级不存在');
        if ($classes['is_end_course'] == 1) $this->error('当前班级已结课');

        //变更签到记录数据
        $model = new ClassesSignModel();
        $title = $id?'编辑':'新增';
        $data['sign_end_time'] = strtotime($data['sign_end_time']);
        if ($id) {
            // 修改
            $detail = $model->where('id', $id)->find();
            if ($data['type'] == 1 && !$detail['content']) {
                $data['content'] = $this->makeCode();
            } else {
                $data['content'] = $detail['content'];
            }
            $res = $detail->save($data);

        } else {
            //新增
            $data['content'] = $this->makeCode();
            $res = $model->save($data);
            if ($res !== false)
                $id = $model->getLastInsID();
            //查询班级学生列表
            $studentList = Student::where('classes_id', $data['classes_id'])->field('uid')->select();
            $insert = [];
            foreach ($studentList as $student) {
                $insert[] = [
                    'uid' => $student['uid'],
                    'sign_id' => $id,
                    'classes_id' => $data['classes_id']
                ];
            }
            $signLogModel = new ClassesSignLog();
            $signLogModel->saveAll($insert);
        }

        if ($res !== false) $this->success($title.'成功', ['id' => $id, 'code' => $data['content']]);
        $this->error($title.'失败');
    }

    public function makeCode()
    {
        return rand(1111,9999);
    }

}